<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\Pivot;

class MenuRole extends Pivot
{
    protected $table = 'menu_role';

    protected $hidden = ['created_at'];
    
    // 转换数组为字符串
    public function setPermissionAttribute($value){
        if (is_array($value) && $value){
            $this->attributes['permission'] = json_encode($value, JSON_UNESCAPED_UNICODE);
        } else {
            $this->attributes['permission'] = null;
        }
    }

    // 转换字符串为数组
    public function getPermissionAttribute($value){
        return $value ? json_decode($value, true) : [];
    }
}
